package com.itheima.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.dao.OrderSettingDao;
import com.itheima.pojo.OrderSetting;
import com.itheima.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import service.OrderSettingService;

import java.util.*;

@Service(interfaceClass = OrderSettingService.class)
@Transactional
public class OrderSettingServiceImpl implements OrderSettingService {
    @Autowired
    private OrderSettingDao orderSettingDao;

    //批量导入预约设置数据
    public void add(List<OrderSetting> list) throws Exception {
        if (list!=null&& list.size()>0){
            for (OrderSetting orderSetting : list) {
                //Date orderDate = orderSetting.getOrderDate();
                //String date2String = DateUtils.parseDate2String(orderDate);
                long count = orderSettingDao.findCountByOrderDate(orderSetting.getOrderDate());
                if (count>0){
                    //已经存在,更新
                    orderSettingDao.editNumberByOrderDate(orderSetting);
                }else {
                    //不存在, 添加
                    orderSettingDao.add(orderSetting);
                }
            }

        }
    }

    //根据月份查询对应的预约数据
    public List<Map> getOrderSettingByMonth(String date) {
        String begin=date+"-1";//2020-6-1
        String end=date+"-31";//2020-6-31
      /*  Map map=new HashMap<>();
        map.put("begin",begin);
        map.put("end",end);*/
        //根据传入的日期进行字符串拼接 真正的去查询
//        List<OrderSetting> list= orderSettingDao.getOrderSettingByMonth(map);

        List<OrderSetting> list= orderSettingDao.getOrderSettingByMonth(begin,end);

        List<Map> result=new ArrayList<>();
        if (list!=null&&list.size()>0){
            for (OrderSetting orderSetting : list) {
                Map<String,Object> m=new HashMap();
                m.put("date",orderSetting.getOrderDate().getDate());
                m.put("number",orderSetting.getNumber());
                m.put("reservations",orderSetting.getReservations());
                result.add(m);
            }
        }
        return result;
    }


    public void editNumberByDate(OrderSetting orderSetting) throws Exception {
       // Date orderDate = orderSetting.getOrderDate();
       // String date2String = DateUtils.parseDate2String(orderDate);
        long count =
                orderSettingDao.findCountByOrderDate(orderSetting.getOrderDate());
        if(count > 0){
        //当前日期已经进行了预约设置，需要进行修改操作
            orderSettingDao.editNumberByOrderDate(orderSetting);
        }else{
        //当前日期没有进行预约设置，进行添加操作
            orderSettingDao.add(orderSetting);
        }

    }


}

